<div style="padding: 5px">
	<ul class="ztree" id="organization_tree">
	
	</ul>
</div>
<script type="text/javascript">
	//@ sourceURL=ogarntree.js
	var organTableId = "${gridId}";
	var level = ${level!};
	//alert(level)
	var setting = {
		async : {
			enable : true,
			url : '${cxt!}/organization/treeData',
			autoParam : [ "id=ids" ],//, "name=n", "level=lv"
			//otherParam : {"otherParam" : "zTreeAsyncTest"},
			type : "post"
		},
		view : {
			expandSpeed : "",
			addHoverDom : addHoverDom,
			removeHoverDom : removeHoverDom,
			dblClickExpand: false,
			selectedMulti : false
		
		},
		edit : {
			enable : false,
			<% if(!authUrl("/organization/delOrgan")){ %>
			showRemoveBtn: false,
			<% } %> 
			showRenameBtn : false,
			removeTitle: "删除"
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			onDblClick : organTreeOnClick,
			beforeRemove : beforeRemove, //节点被删除之前的事件,并且根据返回值确定是否允许删除操作
			beforeExpand : beforeExpand,
			onExpand : onExpand,
			onAsyncSuccess : organTreeOnAsyncSuccess
		}
	};

	var firstAsyncSuccessFlag = 0;
	function organTreeOnAsyncSuccess(event, treeId, msg) {
		if (firstAsyncSuccessFlag == 0) {
			try {
				var organTree = $.fn.zTree.getZTreeObj("organization_tree");
				//调用默认展开第一个结点  
				var nodes = organTree.getNodes();
				organTree.expandNode(nodes[0], true);
				organTree.selectNode(nodes[0]);
				//初始化organtable
				initOrganTable(nodes[0]);

				var childNodes = organTree.transformToArray(nodes[0]);
				organTree.expandNode(childNodes[1], true);
				firstAsyncSuccessFlag = 1;
				
			} catch (err) {

			}

		}
	}

	function organTreeOnClick(event, treeId, treeNode) {
		initOrganTable(treeNode);
	};

	////////////////////下面是处理增删改节点//////////////////

	//节点被删除之前的事件,并且根据返回值确定是否允许删除操作
	function beforeRemove(treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("organization_tree");
		zTree.selectNode(treeNode);
		parent.$.messager.confirm('删除', '确认删除<' + treeNode.name + '>吗？',
				function(r) {
					if (r) {
						var url = "/organization/delOrgan";
						var data = {
							"ids" : treeNode.id
						};
						var reData = common_ajax.ajaxFunc(url, data);
						if (reData == "success") {
							zTree.removeNode(treeNode);
							$("#" + organTableId).datagrid('reload');
						} else {
							parent.$.messager.alert('删除', reData, 'error');
						}
					}

				});

		return false;
	}

	//添加功能按钮
	function addHoverDom(treeId, treeNode) {
		
		var organTree = $.fn.zTree.getZTreeObj("organization_tree");
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0 || $("#operatorBtn_" + treeNode.tId).length > 0) {
			return;
		}
		//2.修改机构
		//alert(treeNode.levelNum !=level&&treeNode.levelNum != 2)
		//alert(treeNode.levelNum ==1)
		//if((treeNode.levelNum !=level&&treeNode.levelNum != 2)||treeNode.levelNum ==1){
		if(treeNode.levelNum !=level||treeNode.levelNum ==1){
		<% if(authUrl("/organization/toUpdate")){ %>
		var operatorStr = "<span class='button edit' id='operatorBtn_"
				+ treeNode.tId + "' title='编辑' onfocus='this.blur();'></span>";
		sObj.after(operatorStr);
		var btn = $("#operatorBtn_" + treeNode.tId);
		if (btn)
			btn.bind("click", function() {
				organTree.selectNode(treeNode);
				var url = 'organization/toUpdate?ids=' + treeNode.id;
				loadDialog($("#" + organTableId), organTree, '编辑', url,1000,550);
			});
		<% } %>
		}
		//2.添加机构
		if(treeNode.levelNum < 4){
			<% if(authUrl("/organization/toAdd")){ %>
			var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
					+ "' title='添加' onfocus='this.blur();'></span>";
			sObj.after(addStr);
			var btn = $("#addBtn_" + treeNode.tId);
			if (btn)
				btn.bind("click", function() {
					//alert( treeNode.id)
					//alert( treeNode.name)
					// 					$("#dlg").dialog("open");
					loadDialog($("#" + organTableId), organTree, '添加','organization/toAdd?ids=' + treeNode.id,1000,550);
				});
			<% } %>
		}
	};

	function removeHoverDom(treeId, treeNode) {
		//1.处理添加按钮
		if(treeNode.levelNum < 4){
			$("#addBtn_" + treeNode.tId).unbind().remove();
		}
		//2.处理查看按钮
		$("#operatorBtn_" + treeNode.tId).unbind().remove();
	};

	////////////////////下面是处理展开//////////////////

	var curExpandNode = null;
	function beforeExpand(treeId, treeNode) {
		var pNode = curExpandNode ? curExpandNode.getParentNode() : null;
		var treeNodeP = treeNode.parentTId ? treeNode.getParentNode() : null;
		var zTree = $.fn.zTree.getZTreeObj("organization_tree");
		for (var i = 0, l = !treeNodeP ? 0 : treeNodeP.children.length; i < l; i++) {
			if (treeNode !== treeNodeP.children[i]) {
				zTree.expandNode(treeNodeP.children[i], false);
			}
		}
		while (pNode) {
			if (pNode === treeNode) {
				break;
			}
			pNode = pNode.getParentNode();
		}
		if (!pNode) {
			singlePath(treeNode);
		}

	}
	function singlePath(newNode) {
		if (newNode === curExpandNode)
			return;

		var zTree = $.fn.zTree.getZTreeObj("organization_tree"), rootNodes, tmpRoot, tmpTId, i, j, n;

		if (!curExpandNode) {
			tmpRoot = newNode;
			while (tmpRoot) {
				tmpTId = tmpRoot.tId;
				tmpRoot = tmpRoot.getParentNode();
			}
			rootNodes = zTree.getNodes();
			for (i = 0, j = rootNodes.length; i < j; i++) {
				n = rootNodes[i];
				if (n.tId != tmpTId) {
					zTree.expandNode(n, false);
				}
			}
		} else if (curExpandNode && curExpandNode.open) {
			if (newNode.parentTId === curExpandNode.parentTId) {
				zTree.expandNode(curExpandNode, false);
			} else {
				var newParents = [];
				while (newNode) {
					newNode = newNode.getParentNode();
					if (newNode === curExpandNode) {
						newParents = null;
						break;
					} else if (newNode) {
						newParents.push(newNode);
					}
				}
				if (newParents != null) {
					var oldNode = curExpandNode;
					var oldParents = [];
					while (oldNode) {
						oldNode = oldNode.getParentNode();
						if (oldNode) {
							oldParents.push(oldNode);
						}
					}
					if (newParents.length > 0) {
						zTree.expandNode(oldParents[Math.abs(oldParents.length
								- newParents.length) - 1], false);
					} else {
						zTree.expandNode(oldParents[oldParents.length - 1],
								false);
					}
				}
			}
		}
		curExpandNode = newNode;
	}

	function onExpand(event, treeId, treeNode) {
		curExpandNode = treeNode;
	}

	//////////////////初始化////////////////////

	$(document).ready(function() {
		$.fn.zTree.init($("#organization_tree"), setting);
	});
</script>